import Vue from 'vue'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import locale from 'element-ui/lib/locale/lang/zh-CN' // lang i18n
// import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'
import storage from '@/utils/storage'
// import { GetUrlRelativePath, showNotice } from '@/utils/toolclass'

import '@/icons' // icon
import BaiduMap from 'vue-baidu-map'
import flvjs from 'flv.js/dist/flv'
import moment from 'moment'
import echarts from 'echarts'
import CountTo from 'vue-count-to'
import md5 from 'js-md5'

// import '@/permission' // permission control

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online ! ! !
 */
if (process.env.NODE_ENV === 'production') {
  const { mockXHR } = require('../mock')
  mockXHR()
}

Vue.prototype.$md5 = md5

Vue.prototype.$moment = moment
Vue.use(flvjs)

Vue.use(echarts)
Vue.use(CountTo)
// set ElementUI lang to EN
Vue.use(ElementUI, { locale })
Vue.use(BaiduMap, {
  // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
  ak: 'k007NSYtuZzFz0PnG8YifooN4UmeTXlp'
})
// 如果想要中文版 element-ui，按如下方式声明
// Vue.use(ElementUI)

Vue.prototype.$storage = storage
Vue.config.productionTip = false
let isRefresh = false

router.beforeEach((to, from, next) => {
  // console.log(to, from)
  if (to.path === '/' || to.name === 'login') {
    storage.clear()
  }
  window.onpopstate = function() {
    // console.log(1111)
    if (from.path === '/') {
      history.go(0)
    } else {
      next()
    }
  }
  if (storage) {
    if (isRefresh) {
      next()
    } else if (store.state.routerList.length < 10 && !isRefresh) {
      isRefresh = true
      store.dispatch('changeRouter').then(res => { // 拉取用户信息
        // console.log(res)
        if (to.name === 'login') {
          storage.clear()
          // console.log('storage', storage)
          next()
        } else {
          if (storage.get('upUrl')) {
            var c = JSON.parse(localStorage.getItem('upUrl'))
            const d = JSON.parse(c)
            next({ path: d.path })
          } else {
            next()
          }
        }
      }).catch(() => {
        // console.log(err)
      })
    }
  } else if (to.name === 'login') {
    next()
  } else {
    // console.log('跳转登录')
    next()
  }
})
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
